package com.blb.controller;


import com.blb.entity.Label;
import com.blb.entity.NoteLabel;
import com.blb.entity.Notes;
import com.blb.service.ILabelService;
import com.blb.service.INoteLabelService;
import com.blb.service.INotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lifeng
 * @since 2019-12-20
 */
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private ILabelService iLabelService;
    @Resource
    private INotesService iNotesService;
    @Resource
    private INoteLabelService iNoteLabelService;

    /**
     * 标签列表
     * @return
     */
    
    @RequestMapping("labelList")
    public List<Label> labelList(String token){
        System.out.println("asdfsaf");
        List<Label> list = iLabelService.listLabel(token);
        return list;
    }

    /**
     * 添加标签
     * @param label
     */
    @RequestMapping("addLabel")
    public void addLabel(@RequestBody Label label){
        label.setId(UUID.randomUUID().toString().replace("-",""));
        label.setUserId(label.getUserId());//登录之后
        iLabelService.save(label);
    }

    /**
     * 删除标签
     * @param label
     */
    @PostMapping("delLabel")
    public void delLabel(@RequestBody Label label){
        String token = label.getUserId();
        String labelId = iLabelService.delGetLabelId(label.getLabelName(), token);//LabelId
        iLabelService.removeById(labelId);//删除标签表
        iLabelService.delNoteLabelByLabel(labelId);//删除关系表中
    }

    /**
     * 通过笔记id和标签id删除关系表
     * @param label
     */
    @PostMapping("delLabel2")
    public void delLabel2(Label label,String token){
        System.out.println("aaaaaaaaaa");
        String token2 = label.getUserId();
        String labelId = iLabelService.delGetLabelId(label.getLabelName(), token2);//LabelId
        String noteName = label.getId();//通过笔记名称查出笔记id
        String noteId = iLabelService.byNoteNameGetNoteId(noteName,token2);
        iLabelService.delLabel2(labelId,noteId);//删除关系表中
    }

    /**
     * 修改标签名
     * @param label
     */
    @PostMapping("updateLabel")
    public void updateLabel(@RequestBody Label label){
        String token = label.getUserId();
        System.out.println("token"+token);
        String labelId = iLabelService.delGetLabelId(label.getId(), token);//LabelId
        System.out.println("labelId"+labelId);
        label.setId(labelId);
        System.out.println("label"+label);
        iLabelService.updateLabel(label);
    }

    /**
     * 通过标签找到关联的笔记
     * @param label
     * @return
     */
    
    @RequestMapping("byLabelGetNote")
    public List<Notes> byLabelGetNote(Label label,String token){
        String labelId = iLabelService.delGetLabelId(label.getLabelName(), token);//根据用户id和标签id一起查询删除
        List<String> noteIds = iLabelService.byLabelGetNote(labelId);
        List<Notes> notes = new ArrayList<Notes>();
        for (String noteId : noteIds){
            Notes note = iNotesService.getById(noteId);
            notes.add(note);
        }
        System.out.println("note"+notes);
        return notes;
    }
    //通过笔记找到相应的标签
    @RequestMapping("byNoteGetLabels")
    public List<Label> byNoteGetLabels(Notes notes,String token){
        String notesName = notes.getNotesName();
        String notesId = iLabelService.byNoteNameGetNoteId(notesName,token);//通过notesName找到notesId
        List<String> labelIds = iLabelService.byNoteGetLabels(notesId);
        List<Label> notes1 = new ArrayList<Label>();
        for (String i : labelIds){
            Label byId = iLabelService.getById(i); //通过标签id找到标签name
            notes1.add(byId);
        }
        System.out.println("notes1"+notes1);
        return notes1;
    }

    /**
     * 笔记添加标签
     * @param label
     */
    @PostMapping("addLabel2")
    public int addLabel2(Label label,String token) {
        int a;
        System.out.println("ff" + label);
        String noteName = label.getId();//笔记名
        String noteId = iLabelService.byNoteNameGetNoteId(noteName,token);
        //String userId = "1";//登录之后
        String labelName = label.getLabelName();
        String labelId1 = iLabelService.delGetLabelId(labelName, token);//关系表
        String labelNum = iLabelService.getLabel2(labelName, token);
        int relationNum = Integer.parseInt(iLabelService.getRelation(labelId1, noteId));
        //先找有没有这个标签，如果没有这个标签就添加标签，并且添加到关系表
        if (labelNum.equals("0")) {
            System.out.println("没有这个标签");
            String labelId = UUID.randomUUID().toString().replace("-", "");
            label.setId(labelId);
            label.setUserId(token);
            iLabelService.save(label);//添加到标签表
            NoteLabel noteLabel = new NoteLabel();
            String id = UUID.randomUUID().toString().replace("-", "");//关系表id
            noteLabel.setId(id);
            noteLabel.setLabelId(labelId);
            noteLabel.setNotesId(noteId);
            noteLabel.setUserId(token);
            iNoteLabelService.save(noteLabel);//添加到关系表
            a = 1;
        }else if(relationNum>0){
            System.out.println("有这个标签，关系表也有");
            a = 2;
        }else{
            //如果有这个标签，就直接添加到关系表
            System.out.println("有这个标签，但关系表没有");
            String labelId = iLabelService.delGetLabelId(label.getLabelName(), token);//找到标签id
            NoteLabel noteLabel = new NoteLabel();
            String id= UUID.randomUUID().toString().replace("-", "");//关系表id
            noteLabel.setId(id);
            noteLabel.setLabelId(labelId);
            noteLabel.setNotesId(noteId);
            iNoteLabelService.save(noteLabel);//添加到关系表
            a = 3;
        }
        return a;
    }


    //手机端方法---------------------------------

    /**
     * 添加标签
     * @param label
     */
    @PostMapping("addLabels")
    public void addLabels(@RequestBody Label label){
        label.setId(UUID.randomUUID().toString().replace("-",""));
        iLabelService.save(label);
    }


    /**
     * 标签列表以及对应的笔记数量
     * @return
     */
    @RequestMapping("mylabelList")
    public List<Label> mylabelList(String token){
        return iLabelService.MyLabelList(token);
    }

    /**
     * 笔记对应的标签id
     * @return
     */
    @PostMapping("thislabelList")
    public List<Label> thislabelList(@RequestBody String notesId){
        List<Label> labels = iLabelService.thisLabelid(notesId.substring(1,notesId.length()-1));
        return labels;
    }

    @PostMapping("del")
    public void del(@RequestBody Label label){
        iLabelService.removeById(label.getId());
    }

    @PostMapping("update")
    public void update(@RequestBody Label label){
        iLabelService.updateById(label.getId(),label.getLabelName());
    }

    @PostMapping("addThisLabel")
    public void addThisLabel(@RequestBody Label label){
        String replace = UUID.randomUUID().toString().replace("-", "");
        label.setId(replace);
        iLabelService.save(label);
        iLabelService.addThisLabel(UUID.randomUUID().toString().replace("-",""),replace,label.getNotesId(),label.getUserId());
    }

    @PostMapping("delThisLabel")
    public void delThisLabel(@RequestBody Label label){
        iLabelService.delThisLabel(label.getId(),label.getNotesId());
    }

    @PostMapping("updateThisLabel")
    public void updateThisLabel(@RequestBody Label label){
        System.out.println(label.getLabelName());
        if (label.getLabelName() == "1"){
            iLabelService.addThisLabel(UUID.randomUUID().toString().replace("-",""),label.getId(),label.getNotesId(),label.getUserId());
        }else {
            iLabelService.delThisLabel(label.getId(),label.getNotesId());
        }

    }



    //手机端方法---------------------------------


}
